package com.sud.demo.service.impl;

import com.sud.demo.model.User;
import com.sud.demo.service.UserService;
import com.sud.framework.mybatis.service.impl.BaseServiceImpl;
import com.sud.framework.system.httpUtil.QueryBase;
import com.sud.framework.system.httpUtil.Result;
import com.sud.framework.system.httpUtil.ResultCode;
import com.sun.jersey.core.header.FormDataContentDisposition;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

/**
 * 功能：
 * 作者：SUD
 * 时间：2018/6/26 14:32
 */
@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {
    @Override
    public Result saveUser(User user) {

        Result result = null;
        try {
            if(StringUtils.isEmpty(user.getUserName())){
                result = Result.error("userName不能为空");
                return result;
            }
            int count = this.insert("user.saveUser", QueryBase.init("user",user));
            if(count>0){
                result = Result.ok();
            }else{
                result = Result.error("保存失败");
            }
        }catch (Exception e){
            result = Result.error(ResultCode.系统错误,e.getMessage());
        }
        return result;
    }

    @Override
    public Result saveUserList(List<User> list) {
        Result result = null;
        try {
            if(list!=null && !list.isEmpty()){
                int count = this.insert("user.bathSaveUser",QueryBase.init("list",list));
                if(count>0){
                    result = Result.ok();
                }else{
                    result = Result.error("保存失败");
                }
            }else{
                result = Result.error("集合不能为空");
            }
        }catch (Exception e){
            result = Result.error(ResultCode.系统错误,e.getMessage());
        }
        return result;
    }

    @Override
    public Result removeUser(Integer userId) {
        Result result = null;
        try {
            if(userId!=null){
                int count = this.delete("user.removeUser",QueryBase.init("userId",userId));
                if(count>0){
                    result = Result.ok();
                }else{
                    result = Result.error("删除失败");
                }
            }else{
                result = Result.error("userId不能为空");
            }
        }catch (Exception e){
            result = Result.error(ResultCode.系统错误,e.getMessage());
        }
        return result;
    }

    @Override
    public Result updateUser(User user) {
        Result result = null;
        try {
            if(user!=null){
                int count = this.update("user.updateUser",QueryBase.init("user",user));
                if(count>0){
                    result = Result.ok();
                }else{
                    result = Result.error("编辑失败");
                }
            }else{
                result = Result.error("编辑对象不能为空");
            }
        }catch (Exception e){
            result = Result.error(ResultCode.系统错误,e.getMessage());
        }
        return result;
    }

    @Override
    public Result getUserList(Integer userId) {
        Result result = null;
        try {
            List<User> list = this.selectObjects("user.selectUserById",QueryBase.init("userId",userId));
            result = Result.ok();
            result.put("list",list);
        }catch (Exception e){
            result = Result.error(ResultCode.系统错误,e.getMessage());
        }
        return result;
    }
    @Override
    public Result getUserPage(String userName,Integer currentPage,Integer pageSize,String sort) {
        Result result = null;
        try {
            int state = currentPage * pageSize;
            QueryBase queryBase = QueryBase.init("userName",userName).put("state",state);
            queryBase.put("pageSize",pageSize).put("sort",sort);

            List<User> list = this.selectObjects("user.selectUserPage",queryBase);
            long count = this.selectObject("user.countUserPage",queryBase);
            result = Result.ok();
            result.put("page",new PageImpl(list,null,count));
        }catch (Exception e){
            result = Result.error(ResultCode.系统错误,e.getMessage());
        }
        return result;
    }
    @Override
    public Result uploadFile(InputStream fileInputStream, FormDataContentDisposition disposition, HttpServletRequest request) {
        return null;
    }
}
